"""表情符号工具类，用于在日志和断言中添加表情符号"""


class EmojiUtils:
    # 测试状态相关表情
    SUCCESS = "✅"  # 成功
    FAILURE = "❌"  # 失败
    WARNING = "⚠️"  # 警告
    INFO = "ℹ️"  # 信息
    DEBUG = "🔍"  # 调试
    ERROR = "🔥"  # 错误

    # 请求相关表情
    REQUEST = "🚀"  # 请求
    RESPONSE = "📩"  # 响应
    SLOW_RESPONSE = "🐢"  # 慢响应
    FAST_RESPONSE = "⚡"  # 快响应

    # 断言相关表情
    ASSERT = "🔍"  # 断言
    ASSERT_EQUALS = "🟰"  # 等于断言
    ASSERT_CONTAINS = "🔎"  # 包含断言
    ASSERT_REGEX = "📝"  # 正则断言
    ASSERT_STATUS = "🔢"  # 状态码断言
    ASSERT_PASS = "✅"  # 断言通过
    ASSERT_FAIL = "❌"  # 断言失败

    # 数据相关表情
    DATA_EXTRACT = "📤"  # 数据提取
    EXTRACT = "📤"  # 数据提取（别名，兼容性用）
    DATA_GLOBAL = "🌐"  # 全局数据
    DATA_LOCAL = "📍"  # 局部数据
    JSON_DATA = "📋"  # JSON数据

    # 测试流程相关表情
    TEST_START = "🏁"  # 测试开始
    TEST_END = "🏆"  # 测试结束
    TEST_SKIP = "⏭️"  # 测试跳过
    TEST_STEP = "👣"  # 测试步骤

    # HTTP方法相关表情
    GET = "📥"  # GET请求
    POST = "📤"  # POST请求
    PUT = "📝"  # PUT请求
    DELETE = "🗑️"  # DELETE请求
    PATCH = "🩹"  # PATCH请求
    HEAD = "👀"  # HEAD请求
    OPTIONS = "🔄"  # OPTIONS请求

    # 其他常用表情
    TIME = "⏱️"  # 时间
    URL = "🔗"  # URL
    HEADER = "📋"  # 请求头
    BODY = "📄"  # 请求体
    COOKIE = "🍪"  # Cookie
    DATABASE = "💾"  # 数据库
    CONFIG = "⚙️"  # 配置
    VARIABLE = "🔖"  # 变量
    FUNCTION = "⚙️"  # 函数
    EXPECTED = "🎯"  # 期望结果
    ACTUAL = "📊"  # 实际结果
    COMPARE = "⚖️"  # 比较
    JSON_PATH = "🔍"  # JSON路径

    @staticmethod
    def get_http_method_emoji(method):
        """根据HTTP方法返回对应的表情符号"""
        method = method.upper() if method else ""
        method_emojis = {
            "GET": EmojiUtils.GET,
            "POST": EmojiUtils.POST,
            "PUT": EmojiUtils.PUT,
            "DELETE": EmojiUtils.DELETE,
            "PATCH": EmojiUtils.PATCH,
            "HEAD": EmojiUtils.HEAD,
            "OPTIONS": EmojiUtils.OPTIONS
        }
        return method_emojis.get(method, "🔄")

    @staticmethod
    def get_status_emoji(status_code):
        """根据HTTP状态码返回对应的表情符号"""
        if not status_code:
            return EmojiUtils.INFO

        if isinstance(status_code, str):
            try:
                status_code = int(status_code)
            except ValueError:
                return EmojiUtils.INFO

        if 200 <= status_code < 300:
            return EmojiUtils.SUCCESS
        elif 300 <= status_code < 400:
            return EmojiUtils.INFO
        elif 400 <= status_code < 500:
            return EmojiUtils.WARNING
        elif 500 <= status_code < 600:
            return EmojiUtils.ERROR
        else:
            return EmojiUtils.INFO

    @staticmethod
    def get_response_time_emoji(response_time_ms):
        """根据响应时间返回对应的表情符号"""
        if not response_time_ms or not isinstance(response_time_ms, (int, float)):
            return EmojiUtils.TIME

        if response_time_ms < 100:
            return EmojiUtils.FAST_RESPONSE
        elif response_time_ms < 1000:
            return EmojiUtils.RESPONSE
        else:
            return EmojiUtils.SLOW_RESPONSE

    @staticmethod
    def get_assert_emoji(assert_type):
        """根据断言类型返回对应的表情符号"""
        assert_emojis = {
            "equals": EmojiUtils.ASSERT_EQUALS,
            "equal_to": EmojiUtils.ASSERT_EQUALS,
            "contains": EmojiUtils.ASSERT_CONTAINS,
            "contains_string": EmojiUtils.ASSERT_CONTAINS,
            "regex": EmojiUtils.ASSERT_REGEX,
            "matches_regexp": EmojiUtils.ASSERT_REGEX,
            "status": EmojiUtils.ASSERT_STATUS,
            "status_code": EmojiUtils.ASSERT_STATUS,
            "json_path": EmojiUtils.JSON_DATA,
            "header": EmojiUtils.HEADER,
            "cookie": EmojiUtils.COOKIE,
            "body": EmojiUtils.BODY,
            "default": EmojiUtils.ASSERT
        }
        return assert_emojis.get(str(assert_type).lower() if assert_type else "default", assert_emojis["default"])

    @staticmethod
    def get_extract_emoji(extract_type):
        """根据提取类型返回对应的表情符号"""
        extract_emojis = {
            "json_path": EmojiUtils.JSON_DATA,
            "header": EmojiUtils.HEADER,
            "cookie": EmojiUtils.COOKIE,
            "body": EmojiUtils.BODY,
            "status_code": EmojiUtils.ASSERT_STATUS,
            "global": EmojiUtils.DATA_GLOBAL,
            "local": EmojiUtils.DATA_LOCAL,
            "default": EmojiUtils.DATA_EXTRACT
        }
        return extract_emojis.get(str(extract_type).lower() if extract_type else "default", extract_emojis["default"])

    @staticmethod
    def format_assert_message(assert_type, expected, actual, passed=True):
        """格式化断言消息，添加表情符号"""
        emoji = EmojiUtils.get_assert_emoji(assert_type)

        return f"断言[{emoji} {assert_type}]: {EmojiUtils.EXPECTED} 期望结果: {expected} | {EmojiUtils.ACTUAL} 实际结果: {actual}"

    @staticmethod
    def format_assert_message_with_path(assert_type, expected, actual, passed=True, path_or_regex=None):
        """格式化带有路径或正则表达式的断言消息，添加表情符号"""
        emoji = EmojiUtils.get_assert_emoji(assert_type)

        base_message = f"断言[{emoji} {assert_type}]"

        if path_or_regex:
            path_desc = "路径" if assert_type == "json_path" else "正则"
            base_message += f" [{path_desc}: {path_or_regex}]"

        base_message += f": {EmojiUtils.EXPECTED} 期望结果: {expected} | {EmojiUtils.ACTUAL} 实际结果: {actual}"

        return base_message

    @staticmethod
    def format_extract_message(extract_type, var_name, value, scope="局部变量", key=None):
        """格式化提取消息，添加表情符号
        
        Args:
            extract_type: 提取类型，如json_path, header, cookie等
            var_name: 变量名
            value: 提取的值
            scope: 作用域，如"全局变量"或"局部变量"
            key: 提取的键名，如json路径、header名等
            
        Returns:
            格式化后的消息字符串
        """
        emoji = EmojiUtils.get_extract_emoji(extract_type)
        scope_emoji = EmojiUtils.DATA_GLOBAL if scope == "全局变量" else EmojiUtils.DATA_LOCAL

        # 构建基本消息
        message = f"{EmojiUtils.DATA_EXTRACT} 提取[{emoji} {extract_type}]"

        # 如果有键名，添加到消息中
        if key:
            message += f" [{key}]"

        # 添加变量名和值
        message += f": {scope_emoji} {var_name} = {value}"

        return message
